$(function() {
    $('#Commonbottom').load('../assets/public/html/PublicBottom.html')
        //弹窗
    toastr.options = {
        "closeButton": true,
        "debug": false,
        "positionClass": "toast-bottom-right",
        "onclick": null,
        "showDuration": "300",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }

    function errorWindows(text1, text2, text3) {
        toastr.error(`${text2}`, `${text1}`, `${text3}`)
    }

    function successWindows(text1, text2) {
        toastr.success(`${text2}`, `${text1}`)
    }

    //导航栏滚动
    $('.user_nav ul li').click(function() {
            $(this).addClass('user_nav_current').siblings().removeClass('user_nav_current')
            let index = $(this).index();
            let rollTop = $('.rollBox').eq(index).offset().top;
            $('html,body').stop().animate({
                scrollTop: rollTop
            })
        })
        //网页滚动时左边导航栏的响应
    $(window).scroll(function() {
        for (let i = 0; i < $('.rollBox').length; i++) {
            //每个rollbox的高度
            const rollBoxHeight = $('.rollBox').eq(i).offset().top
            if ($(window).scrollTop() == 0) {
                $('.user_nav ul li').eq(0).addClass('user_nav_current').siblings().removeClass('user_nav_current')

            }
            if ($(window).scrollTop() >= rollBoxHeight) {
                $('.user_nav ul li').eq(i).addClass('user_nav_current').siblings().removeClass('user_nav_current')
            }
        }

    })

    //时间戳转换为时间
    function getLocalTime(nS) {
        const date = new Date(parseInt(nS) * 1000);
        y = date.getFullYear()
        m = date.getMonth() + 1;
        d = date.getDate()
        const bithdayDate = y + '-' + m + '-' + d
        return bithdayDate
    }

    //调用用户信息接口数据
    function initializationUserCom() {
        const getUserAttr = Interceptor('/getUserAttr', 'post')
        $('#modyifPhoneText').text(getUserAttr.data[0].phone)
        return getUserAttr
    }

    const getUserAttr = initializationUserCom()
        //渲染用户数据
    let getUserObj = {
        //用户基本信息
        basicUserCom: function(res) {
            let birthdayTime = res.data[0].birthday;
            //userMsg男女响应
            let sexImg = '';
            let sexText = '';
            if (res.data[0].sex == 0) {
                sexImg = 'https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/woman.png'
                sexText = '女'
            } else {
                sexImg = 'https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/man.png'
                sexText = '男'
            }

            let conversionTime = ''

            if (birthdayTime == '未填写') {
                conversionTime = '未填写'
            } else {
                conversionTime = getLocalTime(birthdayTime)
            }

            let year = res.data[0].age;
            year = parseInt(year)
            $('.user_header_img>img').attr('src', res.data[0].header)

            const userMsg = `
                <div><b style="font-size: 20px">${res.data[0].username}</b> <span>${res.data[0].identity}</span></div>
                        <div style="margin-top: 10px;">
                            <p>
                                <img src='${sexImg}' alt="" class="icon"><span class="user_sex">${sexText}</span>
                            </p>

                            <p style="margin: 0 20px">
                                <img src="https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/tree.png" alt="" class="icon">
                                <span class="user_age">${year}岁</span>
                            </p>

                            <p>
                                <img src="https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/birthday.png"alt="" class="icon" style="vertical-align: top">
                                <span class="user_birthday">${conversionTime}
                                </p>
                        </div>
                        <div><img src="https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/phone-gray.png"
                                  alt="" class="icon"><span>${res.data[0].phone}</span></div>
                        <div><img src="https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/email-gray.png"
                                  alt="" class="icon"> <span>${res.data[0].email}</span></div>
        `
            $('.overview .user_msg').html(userMsg)

            const informationStr = `
                        <li>
                            <div>姓名:</div>

                            <div>${res.data[0].username}</div>
                            <input type="text" class="user_name edit_ipt" placeholder="请填写姓名 例如:张三">
                        </li>
                        <li>
                            <div>身份:</div>
                            <div>${res.data[0].identity}</div>
                            <input type="text" class="user_identity edit_ipt" placeholder="请填写身份 例如:员工">
                        </li>
                        <li>
                            <div>性别:</div>
                            <div id="sextextbox">${sexText}</div>
                            <div class="sexBox" style="margin-left: 10px;">
                        <span data-value="1"><font>男</font><i></i></span>
                        <span data-value="0"><font>女</font><i></i></span>
                            </div>

                        </li>
                        <li>
                            <div>生日:</div>
                            <div>${conversionTime}</div>
                            <input type="date" class="edit_ipt" id="bithdayIpt">
                        </li>
                        <li>
                            <div>邮箱:</div>
                            <div>${res.data[0].email}</div>
                            <input type="email" class="edit_ipt" placeholder="邮箱地址">

                        </li>
                        <li>
                            <div>电话号码:</div>
                            <div>${res.data[0].phone}</div>
                        </li>
                        <li>
                            <div>常在工作地:</div>
                            <div>${res.data[0].workplace}</div>
                            <input type="text" class="edit_ipt" placeholder="常在工作地">
                        </li>
        `
            $('.personal_information .pconul').html(informationStr)

            if (res.data[0].sex == 0) {
                $('.sexBox span').eq(1).addClass('cur').siblings().removeClass('cur')
            } else {
                $('.sexBox span').eq(0).addClass('cur').siblings().removeClass('cur')
            }
            return this;
        },
        //用户状态
        userStatul: function(res) {
            const user_statul = res.data[0].identity;
            const user_status = res.data[0].status;
            $('.user_statul span').text(user_statul)
            $('.user_statur h2 .status_text').text(user_status)
            return this;

        },
        //个人能力
        personalAbilityCom: function(res) {
            const personal_abilityStr = `<p class="personal_ability_text">${res.data[0].ability}</p><textarea class="abilityTextarea" placeholder="填写职业技能、擅长的事情、喜欢的事情等"></textarea>`
            $('.personal_ability div').html(personal_abilityStr);
            return this;

        },
        //个人优势
        personalAdvantagCom: function(res) {
            const personal_advantagStr = `<p class="personal_advantag_text">${res.data[0].advantage}</p><textarea class="advantagTextarea" placeholder="填写职业技能、擅长的事情、喜欢的事情等"></textarea>`;
            $('.personal_advantag div').html(personal_advantagStr);
            return this;

        },
        //期待职位
        expectedCom: function(res) {
            const expectedStr = `
 
                       <li>
                            <div>${res.data[0].exp_job}</div>
                            <input type="text" class="edit_iptCom expectedPostionIpt" placeholder="职位名称 例:安卓程序员">
                        </li>
                        <li>
                            <div>薪资要求:</div>
                            <div>${res.data[0].exp_moeny}</div>
                            <input type="text" class="edit_iptCom expectedSalaryIpt" placeholder="薪资要求">
                        </li>
                        <li>
                            <div>工作性质:</div>
                            <div>${res.data[0].exp_nature}</div>
                            <input type="text" class="edit_iptCom expectedNatureIpt" placeholder="工作性质 例:全职">
                        </li>
                 

        `
            $('.expected .pconul').html(expectedStr)
            return this;

        },
        //公司名称
        companyCom: function(res) {
            const companyName = res.data[0].company_name
            $('.company_text').text(companyName)

            let injobTime = res.data[0].injob_time;
            let outjobTime = res.data[0].outjob_time;

            if (injobTime == '未填写') {
                injobTime = '未填写'
                outjobTime = '未填写'
            } else {
                injobTime = getLocalTime(injobTime)
                outjobTime = getLocalTime(outjobTime)
            }

            const companyNameStr = `
                        <li>
                            <div>在职时间：</div>
                            <div>${injobTime}/${outjobTime}</div>
                            <input type="date" class="companyDateIpt inJobTime">
                            <input type="date" class="companyDateIpt OutJobTime">
                        </li>
                        <li>
                            <div>公司类型：</div>
                            <div>${res.data[0].com_class}</div>
                            <input type="text" class="edit_iptCom companyTypeIpt" placeholder="公司类型 例:电商">

                        </li>
                        <li>
                            <div>所属部门：</div>
                            <div>${res.data[0].department}</div>
                            <input type="text" class="edit_iptCom companyDepartmentIpt" placeholder="所属部门 例:开发部">

                        </li>
                        <li>
                            <div>职位名称：</div>
                            <div>${res.data[0].job}</div>
                            <input type="text" class="edit_iptCom companyPostionIpt" placeholder="职位名称 例:Java工程师">
                        </li>
        `
            $('.company_name .pconul').html(companyNameStr);
            return this;

        },
        //项目经历
        projectCom: function(res) {
            const projectStr = `
                        <li>
                            <div>${res.data[0].project}</div>
                            <input type="text" class="edit_iptCom projectNameIpt" placeholder="项目名称">
                        </li>
                        <li>
                            <div>所属公司：</div>
                            <div>${res.data[0].pro_name}</div>
                            <input type="text" class="edit_iptCom projectCompanyNameIpt" placeholder="所属公司">

                        </li>
                        <li>
                            <div>责任描述：</div>
                            <div>${res.data[0].mypro}</div>
                            <input type="text" class="edit_iptCom projectrEsponsibleIpt" placeholder="职位描述">

                        </li>`
            $('.project .pconul').html(projectStr)
            return this;

        },
        //教育经历
        educationCom: function(res) {
            let inSchoolTime = res.data[0].school_time;
            let outSchoolTime = res.data[0].out_school;

            if (inSchoolTime == '未填写') {
                inSchoolTime = '未填写'
                outSchoolTime = '未填写'
            } else {
                inSchoolTime = getLocalTime(inSchoolTime)
                outSchoolTime = getLocalTime(outSchoolTime)
            }


            const educationStr = `
                        <li>
                            <div>学校名称：</div>
                            <div>${res.data[0].school}</div>
                            <input type="text" class="edit_iptCom schoolNameIpt" placeholder="请填写 例如:北京大学">
                        </li>
                        <li>
                            <div>学历：</div>
                            <div>${res.data[0].level}</div>
                            <input type="text" class="edit_iptCom educationIpt" placeholder="请填写 例如:本科">

                        </li>
                        <li>
                            <div>专业：</div>
                            <div>${res.data[0].subject}</div>
                            <input type="text" class="edit_iptCom majorIpt" placeholder="请填写 例如:计算机网络应用">

                        </li>
                        <li>
                            <div>在校时间段：</div>
                            <div>${inSchoolTime}/${outSchoolTime}</div>
                            <input type="date" class="companyDateIpt school_time">
                            <input type="date" class="companyDateIpt out_school">

                        </li>
                        <li>
                            <div>在校经历：</div>
                            <div>${res.data[0].in_school}</div>
                            <input type="text" class="edit_iptCom experienceIpt" placeholder="在校经历">

                        </li>
                   
        `
            $('.education .pconul').html(educationStr)
            return this;

        },
        //资格证书
        certificateCom: function(res) {
            const certificateStr = `
                        <li>
                            <div>证书名称：</div>
                            <div>${res.data[0].certificate}</div>
                            <input type="text" class="edit_iptCom certificateIpt" placeholder="请填写 例如:程序设计师">
                        </li>
                        <li>
                            <div>证书颁发机构:</div>
                            <div>${res.data[0].mechanism}</div>
                            <input type="text" class="edit_iptCom mechanismIpt" placeholder="请填写 例如:教育局">

                        </li>
                        <li>
                            <div>等级：</div>
                            <div>${res.data[0].grade}</div>
                            <input type="text" class="edit_iptCom gradeIpt" placeholder="请填写 例如:5">
                        </li>
                        <li>
                            <div>是否可查：</div>
                            <div style="color: #FF511C">${res.data[0].is_check}</div>
                            <input type="text" class="edit_iptCom queryIpt" placeholder="请填写 例如:可查">
                        </li>
        `
            $('.certificate .pconul').html(certificateStr)
            return this;

        }
    }


    getUserObj.basicUserCom(getUserAttr)
        .userStatul(getUserAttr)
        .personalAbilityCom(getUserAttr)
        .personalAdvantagCom(getUserAttr)
        .expectedCom(getUserAttr)
        .companyCom(getUserAttr)
        .projectCom(getUserAttr)
        .educationCom(getUserAttr)
        .certificateCom(getUserAttr);

    $('.user_header_img').click(function() {

        $('.change_header_img input').click()
    })

    //修改头像
    function editAvatar() {
        $('.change_header_img input').on("change", function() {
            let form = document.querySelector('.change_header_img')
            const file = this.files[0]
            let formData = new FormData(form)
            const fileSize = 2 * 1024 * 1024; //图片最大
            formData.append('headimg', file)
            if (file.size > fileSize) {
                errorWindows('上传失败，请上传不大于2M的图片！', '')
                return
            } else {
                let headImg = upload('/headImg', 'post', formData);
                const { code, message, data } = headImg
                code != 666 ? errorWindows(message, '') : successWindows(message, ''), $('.user_header_img>img').attr('src', data.header)
            }
        });
    }

    editAvatar();

    //判断个人信息完整度
    function userComplete() {
        usercompleteFlag = 0;
        let div = $('.personal_information ul li div:nth-of-type(2)');
        for (let i = 0; i < div.length; i++) {
            div[i].innerHTML == '未填写' ? usercompleteFlag += 1 : ''
        }
        let projectNum = Math.ceil(100 - (usercompleteFlag * 14.28571428571429));
        $('.progressBgc').css({
            'width': projectNum + '%'
        })
        $('.progress_text').text(projectNum + '%')

        if (projectNum >= 100) {
            $('.integrity p:nth-of-type(1)').text('简历填写完成')
        } else if (projectNum >= 60) {
            $('.integrity p:nth-of-type(1)').text('简历已达到标准')
        } else {
            $('.integrity p:nth-of-type(1)').text('加油，简历还未达到完成标准哦 ！')
        }

    }

    userComplete();

    //编辑按钮更改样式
    const changeEdit = (flag, editDom) => {
        if (flag % 2 != 0) {
            editDom.css('color', '#CDCDCD').html('<img src="https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/pen.png"> 编辑')
        } else {
            editDom.css('color', '#FF511C').html('<img src="https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/icon_dui.png"> 完成')
        }
    }

    //基本个人信息
    let perFlag = 1;
    $('.personal_information .edit').click(function() {

        let userEditIpt = $('.edit_ipt')
        let user_nameIpt = $('.edit_ipt').eq(0);
        let userNameIpt = $('.edit_ipt').eq(0).val();
        let user_name = $('.edit_ipt').eq(0).prev().text();

        let user_IdentityIpt = $('.edit_ipt').eq(1);
        let identityIpt = $('.edit_ipt').eq(1).val();
        let identityIptText = $('.edit_ipt').eq(1).prev().text();

        let user_BirthdayIpt = $('.edit_ipt').eq(2);
        let birthdayIpt = $('.edit_ipt').eq(2).val();
        let birthdayIptText = $('.edit_ipt').eq(2).prev().text();

        let user_EmailIpt = $('.edit_ipt').eq(3);
        let eamilIpt = $('.edit_ipt').eq(3).val();
        let eamliIptText = $('.edit_ipt').eq(3).prev().text();

        let phoneText = $('.personal_information ul li').eq(5).children().eq(1).text();

        let user_AddressIpt = $('.edit_ipt').eq(4);
        let addressIpt = $('.edit_ipt').eq(4).val();
        let addressIptText = $('.edit_ipt').eq(4).prev().text();

        perFlag += 1;
        editIpt(perFlag, user_nameIpt, userNameIpt, userEditIpt, user_name);
        editIpt(perFlag, user_IdentityIpt, identityIpt, userEditIpt, identityIptText)
        editIpt(perFlag, user_BirthdayIpt, birthdayIpt, userEditIpt, birthdayIptText);
        editIpt(perFlag, user_EmailIpt, eamilIpt, userEditIpt, eamliIptText);
        editIpt(perFlag, user_AddressIpt, addressIpt, userEditIpt, addressIptText)

        changeSex()
        changeEdit(perFlag, $(this))

        //获取性别的值
        let sexDate = ''
        for (let i = 0; i < $('.sexBox span').length; i++) {
            if ($($('.sexBox span').eq(i)).hasClass('cur')) {
                sexDate = $($('.sexBox span').eq(i)).attr('data-value')
            }
        }

        //设置input:date的val
        let newBiText = birthdayIptText.split('-');
        for (let i = 0; i < newBiText.length; i++) {
            if (parseInt(newBiText[i]) < 10) {
                newBiText[i] = '0' + newBiText[i]
            }
        }

        newBiText = newBiText.join('-')
        user_BirthdayIpt.attr('value', newBiText)

        if (perFlag % 2 != 0) {
            let birthdayTimestamp = new Date(birthdayIpt).getTime() / 1000; //转换为date，在获取时间戳
            const editUserAttr = Interceptor('/editUserAttr', 'post', {
                'userName': userNameIpt,
                'sex': sexDate,
                'identity': identityIpt,
                'birthday': birthdayTimestamp,
                'email': eamilIpt,
                'phone': phoneText,
                'workplace': addressIpt
            })

            getUserObj.basicUserCom(initializationUserCom())
            userComplete()
            const { code, message } = editUserAttr;
            code != 666 ? errorWindows('基本个人信息', message) : successWindows('基本个人信息', message)
        }
    })


    //当前求职状态
    let statusFlag = 1;
    $('.user_statur .edit').click(function() {
        statusFlag += 1
        let statusIpt = $('.user_statuEdit');
        let statusIptVal = statusIpt.val();
        let statusText = $('.status_text').text();

        editIpt(statusFlag, statusIpt, statusIptVal, statusIpt, statusText);
        changeEdit(statusFlag, $(this))

        if (statusFlag % 2 != 0) {

            const editUserState = Interceptor('/editUserState', 'post', {
                'status': statusIptVal,
            })
            const { code, message } = editUserState;
            getUserObj.userStatul(initializationUserCom())
            if (code != 666) {
                errorWindows('当前求职状态', message)
            } else {
                successWindows('当前求职状态', message)
            }
        }
    })

    //个人能力
    let abliltyFlag = 1;
    $('.personal_ability .edit').click(function() {
        abliltyFlag += 1;
        let abilitIpt = $('.abilityTextarea');
        let abilitIptVal = abilitIpt.val();
        let abilityText = $('.personal_ability_text').text();
        editIpt(abliltyFlag, abilitIpt, abilitIptVal, abilitIpt, abilityText)

        changeEdit(abliltyFlag, $(this))

        if (abliltyFlag % 2 != 0) {
            const editUserState = Interceptor('/editUserState', 'post', {
                'ability': abilitIptVal,
            });
            getUserObj.personalAbilityCom(initializationUserCom())

            const { code, message } = editUserState;

            if (code != 666) {
                errorWindows('个人能力', message)
            } else {
                successWindows('个人能力', message)
            }
        }
    })

    //个人优势
    let advantagFlag = 1
    $('.personal_advantag .edit').click(function() {
        advantagFlag += 1;
        let advantagIpt = $('.advantagTextarea');
        let advantagIptVal = advantagIpt.val();
        let advantagText = $('.personal_advantag_text').text();

        editIpt(advantagFlag, advantagIpt, advantagIptVal, advantagIpt, advantagText);
        changeEdit(advantagFlag, $(this))
        if (advantagFlag % 2 != 0) {
            const editUserState = Interceptor('/editUserState', 'post', {
                'advantage': advantagIptVal,
            })

            getUserObj.personalAdvantagCom(initializationUserCom())

            const { code, message } = editUserState;

            if (code != 666) {
                errorWindows('个人优势', message)
            } else {
                successWindows('个人优势', message)
            }
            advantagIpt.hide()
        } else {
            advantagIpt.show()
        }
    })

    //期待职位
    let expectedFlag = 1;
    $('.expected .edit').click(function() {
        expectedFlag += 1;
        let expectedAllIpt = $('.expected .edit_iptCom');

        let expectedPostionIpt = $('.expectedPostionIpt');
        let expectedPostionIptVal = expectedPostionIpt.val();
        let expectedPostionText = expectedAllIpt.eq(0).prev().text();

        let expectedSalaryIpt = $('.expectedSalaryIpt');
        let expectedSalaryIptVal = expectedSalaryIpt.val();
        let expectedSalaryText = expectedAllIpt.eq(1).prev().text();

        let expectedNatureIpt = $('.expectedNatureIpt');
        let expectedNatureIptVal = expectedNatureIpt.val();
        let expectedNatureIptText = expectedAllIpt.eq(2).prev().text();

        editIpt(expectedFlag, expectedPostionIpt, expectedPostionIptVal, expectedAllIpt, expectedPostionText)
        editIpt(expectedFlag, expectedSalaryIpt, expectedSalaryIptVal, expectedAllIpt, expectedSalaryText)
        editIpt(expectedFlag, expectedNatureIpt, expectedNatureIptVal, expectedAllIpt, expectedNatureIptText)

        changeEdit(expectedFlag, $(this))
        if (expectedFlag % 2 != 0) {

            const editUserState = Interceptor('/editUserState', 'post', {
                'job': expectedPostionIptVal,
                'money': expectedSalaryIptVal,
                'nature': expectedNatureIptVal
            })
            getUserObj.expectedCom(initializationUserCom())
            const { code, message } = editUserState;

            if (code != 666) {
                errorWindows('期待职位', message)
            } else {
                successWindows('期待职位', message)
            }
        }
    })

    //公司名称
    let companyFlag = 1;
    $('.company_name .edit').click(function() {
        companyFlag += 1;

        let companyAllIpt = $('.company_name .edit_iptCom');

        let companyNameIpt = $('.companyNameIpt');
        let companyNameIptVal = companyNameIpt.val();
        let companyNameIptText = $('.company_text').text();


        let companyTimt = $('.inJobTime').prev()
        let companyTimtText = $('.inJobTime').prev().text()
        let companyInjobTimeIpt = $('.inJobTime')
        let companyOutJobTimeIpt = $('.OutJobTime')
        let companyInjobTimeIptVal = companyInjobTimeIpt.val()
        let companyOutJobTimeIptVal = companyOutJobTimeIpt.val()

        let companyTypeIpt = $('.companyTypeIpt');
        let companyTypeIptVal = companyTypeIpt.val();
        let companyTypeIptText = companyAllIpt.eq(1).prev().text();
        let companyDepartmentIpt = $('.companyDepartmentIpt');
        let companyDepartmentIptVal = companyDepartmentIpt.val();
        let companyDepartmentIptText = companyAllIpt.eq(2).prev().text();

        let companyPostionIpt = $('.companyPostionIpt');
        let companyPostionIptVal = companyPostionIpt.val();
        let companyPostionIptText = companyAllIpt.eq(3).prev().text();

        editIpt(companyFlag, companyNameIpt, companyNameIptVal, companyAllIpt, companyNameIptText)
        editDate(companyFlag, companyInjobTimeIpt, companyOutJobTimeIpt, companyTimtText)
        editIpt(companyFlag, companyTypeIpt, companyTypeIptVal, companyAllIpt, companyTypeIptText)
        editIpt(companyFlag, companyDepartmentIpt, companyDepartmentIptVal, companyAllIpt, companyDepartmentIptText)
        editIpt(companyFlag, companyPostionIpt, companyPostionIptVal, companyAllIpt, companyPostionIptText)
        changeEdit(companyFlag, $(this))

        let newDate = companyTimtText.split('/')
        let firstTime = newDate[0].split('-');
        let secondTime = newDate[1].split('-')

        for (let i = 0; i < firstTime.length; i++) {
            if (parseInt(firstTime[i]) < 10) {
                firstTime[i] = '0' + firstTime[i]
            }
            if (parseInt(secondTime[i]) < 10) {
                secondTime[i] = '0' + secondTime[i]
            }
        }

        companyInjobTimeIpt.val(firstTime.join('-'))
        companyOutJobTimeIpt.val(secondTime.join('-'))

        if (companyFlag % 2 != 0) {
            const firstTimeStamp = Date.parse(companyInjobTimeIptVal) / 1000
            const sencodTimeStamp = Date.parse(companyOutJobTimeIptVal) / 1000

            const editTemp = Interceptor('/editTemp', 'post', {
                'tempName': companyNameIptVal,
                'tempClass': companyTypeIptVal,
                'dept': companyDepartmentIptVal,
                'job': companyPostionIptVal,
                'inJob': firstTimeStamp,
                'outJob': sencodTimeStamp
            })
            getUserObj.companyCom(initializationUserCom())
            const { code, message } = editTemp;

            if (code != 666) {
                errorWindows('公司名称', message)
            } else {
                successWindows('公司名称', message)
            }
            companyTimt.show()
        } else {
            companyTimt.hide()
        }
    })

    //教育经历
    let educationFlag = 1;
    $('.education  .edit').click(function() {
        educationFlag += 1;

        let educationAllIpt = $('.education .edit_iptCom');
        let schoolNameIpt = $('.schoolNameIpt');
        let schoolNameIptVal = schoolNameIpt.val();
        let schoolNameIptText = educationAllIpt.eq(0).prev().text();

        let educationIpt = $('.educationIpt');
        let educationIpttVal = educationIpt.val();
        let educationIptText = educationAllIpt.eq(1).prev().text();

        let majorIpt = $('.majorIpt');
        let majorIptVal = majorIpt.val();
        let majorIptText = educationAllIpt.eq(2).prev().text();

        let school_timeIpt = $('.school_time')
        let school_timeIptVal = school_timeIpt.val()
        let out_schoolIpt = $('.out_school')
        let out_schoolIptVal = out_schoolIpt.val()
        let shcoolTimeText = school_timeIpt.prev()


        let experienceIpt = $('.experienceIpt');
        let experienceIptVal = experienceIpt.val();
        let experienceIptText = educationAllIpt.eq(3).prev().text();

        editIpt(educationFlag, schoolNameIpt, schoolNameIptVal, educationAllIpt, schoolNameIptText)
        editIpt(educationFlag, educationIpt, educationIpttVal, educationAllIpt, educationIptText)
        editIpt(educationFlag, majorIpt, majorIptVal, educationAllIpt, majorIptText)
        editDate(educationFlag, school_timeIpt, out_schoolIpt, shcoolTimeText)
        editIpt(educationFlag, experienceIpt, experienceIptVal, educationAllIpt, experienceIptText)
        changeEdit(educationFlag, $(this))

        let newDate = shcoolTimeText.text().split('/');
        let firstTime = newDate[0].split('-');
        let secondTime = newDate[1].split('-');
        let firstTimeStamp = '';
        let sencodTimeStamp = ''
        for (let i = 0; i < firstTime.length; i++) {
            if (parseInt(firstTime[i]) < 10) {
                firstTime[i] = '0' + firstTime[i]
            }
            if (parseInt(secondTime[i]) < 10) {
                secondTime[i] = '0' + secondTime[i]
            }
        }

        school_timeIpt.val(firstTime.join('-'))
        out_schoolIpt.val(secondTime.join('-'))

        if (educationFlag % 2 != 0) {
            firstTimeStamp = Date.parse(school_timeIptVal) / 1000
            sencodTimeStamp = Date.parse(out_schoolIptVal) / 1000
            const editStudy = Interceptor('/editStudy', 'post', {
                'school': schoolNameIptVal,
                'level': educationIpttVal,
                'subject': majorIptVal,
                'schoolTime': firstTimeStamp,
                'outSchool': sencodTimeStamp,
                'inSchool': experienceIptVal
            })
            getUserObj.educationCom(initializationUserCom())
            const { code, message } = editStudy;
            if (code != 666) {
                errorWindows('教育经历', message)
            } else {
                successWindows('教育经历', message)
            }
            shcoolTimeText.show()
        } else {
            shcoolTimeText.hide()
        }
    })

    //项目经历
    let projectFlag = 1;
    $('.project .edit').click(function() {
        projectFlag += 1;
        let projectAllIpt = $('.project .edit_iptCom');

        let projectNameIpt = $('.projectNameIpt');
        let projectNameIptVal = projectNameIpt.val();
        let projectNameIptValText = projectAllIpt.eq(0).prev().text();

        let projectCompanyNameIpt = $('.projectCompanyNameIpt');
        let projectCompanyNameIptVal = projectCompanyNameIpt.val();
        let projectCompanyNameIptText = projectAllIpt.eq(1).prev().text();

        let projectrEsponsibleIpt = $('.projectrEsponsibleIpt');
        let projectrEsponsibleIptVal = projectrEsponsibleIpt.val();
        let projectrEsponsibleIptText = projectAllIpt.eq(2).prev().text();

        editIpt(projectFlag, projectNameIpt, projectNameIptVal, projectAllIpt, projectNameIptValText)
        editIpt(projectFlag, projectCompanyNameIpt, projectCompanyNameIptVal, projectAllIpt, projectCompanyNameIptText)
        editIpt(projectFlag, projectrEsponsibleIpt, projectrEsponsibleIptVal, projectAllIpt, projectrEsponsibleIptText)
        changeEdit(projectFlag, $(this))

        if (projectFlag % 2 != 0) {
            const editProject = Interceptor('/editProject', 'post', {
                'action': 1,
                'project': projectNameIptVal,
                'proName': projectCompanyNameIptVal,
                'myPro': projectrEsponsibleIptVal
            })
            getUserObj.projectCom(initializationUserCom())
            const { code, message } = editProject;
            if (code != 666) {
                errorWindows('项目经历', message)
            } else {
                successWindows('项目经历', message)
            }
        }
    })

    //资格证书
    let certificateFlag = 1;
    $('.certificate  .edit').click(function() {
        certificateFlag += 1;

        let certificateAllIpt = $('.certificate .edit_iptCom');
        let certificateIpt = $('.certificateIpt');
        let certificateIptVal = certificateIpt.val();
        let certificateIptText = certificateAllIpt.eq(0).prev().text();

        let mechanismIpt = $('.mechanismIpt');
        let mechanismIptVal = mechanismIpt.val();
        let mechanismIptText = certificateAllIpt.eq(1).prev().text();

        let gradeIpt = $('.gradeIpt');
        let gradeIptVal = gradeIpt.val();
        let gradeIptText = certificateAllIpt.eq(2).prev().text();

        let queryIpt = $('.queryIpt');
        let queryIptVal = queryIpt.val();
        let queryIptText = certificateAllIpt.eq(3).prev().text();


        editIpt(certificateFlag, certificateIpt, certificateIptVal, certificateAllIpt, certificateIptText)
        editIpt(certificateFlag, mechanismIpt, mechanismIptVal, certificateAllIpt, mechanismIptText)
        editIpt(certificateFlag, gradeIpt, gradeIptVal, certificateAllIpt, gradeIptText)
        editIpt(certificateFlag, queryIpt, queryIptVal, certificateAllIpt, queryIptText)

        changeEdit(certificateFlag, $(this))

        if (certificateFlag % 2 != 0) {
            const editSSL = Interceptor('/editSSL', 'post', {
                'certificate': certificateIptVal,
                'mechanism': mechanismIptVal,
                'grade': parseInt(gradeIptVal),
                'is_check': queryIptVal
            })
            getUserObj.certificateCom(initializationUserCom())
            const { code, message } = editSSL;
            if (code != 666) {
                errorWindows('资格证书', message)
            } else {
                successWindows('资格证书', message)
            }
        }
    })

    //收藏职位
    let collFlag = 1;
    $('.collection  .edit').click(function() {
        collFlag += 1;
        changeEdit(collFlag, $(this));
        if (collFlag % 2 != 0) {
            $('.cancelColl').hide()
        } else {
            $('.cancelColl').show()
        }

    })

    //编辑
    function editIpt(flag, ipt, iptval, iptALL, divMsg) {
        /*
         flag 开关器
         ipt input框
         iptval ipt输入的值
         iptall 所有input框
         divMsg 显示input框内容的盒子
         */
        if (flag % 2 != 0) {
            ipt.prev().show()
            iptALL.hide();
            ipt.prev().val(iptval)
            return
        } else {
            ipt.prev().hide()
            iptALL.show();
            ipt.val(divMsg);
            return;
        }
    }

    //编辑date
    function editDate(flag, ipt, ipt2, iptText) {
        if (flag % 2 != 0) {
            ipt.hide();
            ipt2.hide();
        } else {
            ipt.show();
            ipt2.show()
        }
    }


    //改变性别
    function changeSex() {
        if (perFlag % 2 != 0) {
            $('.sexBox').hide();
            $('#sextextbox').show();
        } else {
            $('.sexBox').show();
            $('#sextextbox').hide();
        }

        $('.sexBox span').click(function() {
            $(this).addClass('cur').siblings().removeClass('cur');
            return $(this).attr('data-value')
        })
    }


    //分页
    //请求分页数据
    function initialization() {
        let getUserColl = Interceptor('/getUserColl', 'post');
        getUserColl.data.current_page = 0
        return getUserColl
    }

    const getUserColl = initialization()
        //初始化数据
    let page = getUserColl.data;

    Initialization();

    //判断收藏职位数
    function pageNull(res) {
        if (res.total == 0) {
            $('.collection ul').css({
                'min-height': 200 + 'px',
                'line-height': 200 + 'px',
                'text-align': 'center'
            }).text('暂无内容')
            $('.paging').hide()
        } else {
            $('.paging').show()
        }
    }

    //请求数据
    function ajaxPage(res) {
        let { total, per_page, current_page, last_page, data } = res;
        return {
            total,
            per_page,
            current_page,
            data,
            last_page
        }
    }

    //将分页内容渲染出来
    function pageCom(pageResult) {
        $('.collection ul').html('');
        $('.collection h2 span').eq(0).text('(' + pageResult.total + ')')

        //将数据中的内容遍历出来
        for (let i = 0; i < pageResult.data.length; i++) {
            // $('.compay_p').length
            if (pageResult.data[i]) { //数据的第i个是否存在
                let pageComStr = `<li>
                           <div class="coll_l">
                               <h3><b>${pageResult.data[i].official}</b> <span>${pageResult.data[i].job_money}</span></h3>
                               <div class="condition">
                                   <div class="condition1" style="display: ${pageResult.data[i].requirement ? 'block' : 'none'}">${pageResult.data[i].requirement}</div>
                                   <div class="condition2" style="display: ${pageResult.data[i].education ? 'block' : 'none'}">${pageResult.data[i].education}</div>
                               </div>

                               <div class="compay_msg">
                                   <img src="${pageResult.data[i].header}"
                                        alt="">
                                   <span>${pageResult.data[i].company_area}</span>
                                   <span>${pageResult.data[i].type}</span>
                               </div>
                           </div>
                           <div class="coll_r">
                               <img src="${pageResult.data[i].header}"
                                    alt="">
                               <div class="compay_n">${pageResult.data[i].username}</div>
                               <div class="compay_p" user_position="${pageResult.data[i].position}"></div>
                           </div>
                           <div class="cancelColl" onclick="cancelCollFn($(this))">
                           取消收藏
                            </div>
                       </li>`

                $('.collection ul').append(pageComStr);
                const postion = parseInt($('.compay_p').eq(i).attr('user_position'))
                switch (postion) {
                    case 0:
                        $('.compay_p').eq(i).text('员工');
                        break;
                    case 1:
                        $('.compay_p').eq(i).text('人事');
                        break;
                    case 2:
                        $('.compay_p').eq(i).text('董事长');
                        break;
                }
            }


        }
        pageNull(pageResult)
    }

    //上一页
    $('.paging_left').click(function() {
        if (page.current_page == 0) {
            console.log('已经在第一页了')
            return
        }
        page.current_page--
            $('.page_item ').eq(page.current_page).addClass('page_item pagin_current').siblings().removeClass('pagin_current')

        let getUserColl = Interceptor('/getUserColl', 'post', {
            "currPage": page.current_page + 1
        });
        pageCom(getUserColl.data)
    })

    //下一页
    $('.paging_rignt').click(function() {
        if (page.current_page >= page.last_page - 1) {
            console.log('已经在最后一页了')
            return
        }
        page.current_page++
            $('.page_item ').eq(page.current_page).addClass('page_item pagin_current').siblings().removeClass('pagin_current')
        let getUserColl = Interceptor('/getUserColl', 'post', {
            "currPage": page.current_page + 1
        });
        pageCom(getUserColl.data)
    })

    //点击页面切换数据
    clickpages = function(i, dom) {
        page.current_page = i
        let num = i + 1
        dom.addClass('pagin_current').siblings().removeClass('pagin_current')

        const getUserColl = Interceptor('/getUserColl', 'post', {
            'currPage': num
        })
        pageCom(getUserColl.data)
    }

    //加载数据
    function Initialization() {
        page = ajaxPage(page)
        pageCom(page)
        pageItem(page)
    }

    //将分页按钮遍历出来
    function pageItem(res) {
        $('.paging_btn').html('');
        for (let i = 0; i < res.last_page; i++) {
            let pageItemStr = `<span class="page_item ${i == res.current_page ? 'pagin_current' : ''}" onclick="clickpages(${i},$(this))">${i + 1}</span>`
            $('.paging_btn').append(pageItemStr)
        }
    }

    //取消收藏
    cancelCollFn = dom => {
        const companyName = dom.prevAll('.coll_l').children('h3').children('b').text()
        const editUserColl = Interceptor('/editUserColl', 'post', {
            'state': 1,
            'offName': companyName
        })
        const { code, message } = editUserColl
        if (code != 666) {
            errorWindows('取消收藏', message)
        } else {
            successWindows('取消收藏', message)
            const getUserColl = initialization()
            let page = getUserColl.data;
            pageCom(page)
            pageItem(page)
            $('.cancelColl').show()
            $('.collection h2 span').eq(0).text('(' + page.total + ')')
        }
    }


    //安全中心
    $('#changePhone').click(modyifPhone);
    $('#changePass').click(modyifPass)
    $('#returnSecurity').click(returnSecurity)


    //修改手机号
    function modyifPhone() {
        $('.security_con').hide();
        $('#modifyPhone').show();
        $('.modifyPhoneTitle').show()
    }

    let sendTime = 60
        //限制短信发送时间
    limitedTime = function(dom) {
        if (sendTime == 0) {
            dom.css({ 'pointer-events': 'auto' })
            dom.prop('disabled', false)
            dom.text('获取验证码');
            sendTime = 60
            return
        } else {
            sendTime--
            dom.css({ 'pointer-events': 'none' })
            dom.prop('disabled', true)
            dom.text(`${sendTime}秒后重新发送`)
        }
        setTimeout(() => {
            limitedTime(dom)
        }, 1000)
    }

    //修改手机号发送验证码
    $('.modifyPhoneSendCode').click(function() {
        let newPhoneVal = $('.newPhone').val();
        let phoneTest = /^[1][3,4,5,7,8,9][0-9]{9}$/;

        if (!phoneTest.test(newPhoneVal)) {
            $('#phoneError').show().text('手机号格式不正确')
        } else {
            $('#phoneError').hide();
            const send = Interceptor('/send', 'post', { 'phone': newPhoneVal })
            const { code, message } = send

            if (code != 666) {
                errorWindows(message, '')
            } else {
                limitedTime($(this))
                successWindows(message, '')
            }
        }
    })

    //修改手机号按钮
    $('#modifyPhoneBtn').click(() => {
        let newPhone = $('.newPhone');
        let newPhoneVal = $('.newPhone').val();
        let phoneTest = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        let phoneFlag = false;

        let codeIpt = $('.newCode');
        let codeIptVal = codeIpt.val();
        let codeTest = /^\d{4}$/;
        let codeFlag = false;

        if (!codeTest.test(codeIptVal)) {
            $('#codeError').show().text('验证码格式不正确');
            codeFlag = false;
        } else {
            $('#codeError').hide();
            codeFlag = true;
        }

        if (!phoneTest.test(newPhoneVal)) {
            $('#phoneError').show().text('手机号格式不正确')
            phoneFlag = false;
        } else {
            $('#phoneError').hide();
            phoneFlag = true;
        }

        if (phoneFlag && codeFlag) {
            const modifyPhone = Interceptor('/modifyPhone', 'post', {
                'phone': newPhoneVal,
                'code': codeIptVal
            })
            const { code, message } = modifyPhone
            if (code == 304) {
                reviseSuccess()
            } else {
                errorWindows('修改手机号', message)
            }
        }
    })

    //修改密码
    function modyifPass() {
        $('.security_con').hide();
        $('#modifyPass').show();
        $('.modifyPassTitle').show()
    }


    //修改密码发送验证码
    $('.modifyPassSendCode').click((e) => {
        let newPassPhoneVal = $('.newPassPhoneIpt').val();
        let phoneTest = /^[1][3,4,5,7,8,9][0-9]{9}$/;

        if (!phoneTest.test(newPassPhoneVal)) {
            $('#passPhoneError').show().text('手机号格式不正确')
        } else {
            $('#passPhoneError').hide();
            const send = Interceptor('/send', 'post', {
                'phone': newPassPhoneVal
            })
            const { code, message } = send
            if (code == 666) {
                successWindows(message, '')
                limitedTime($(e.target))
            } else {
                errorWindows(message, '')
            }
        }
    })

    //修改密码按钮
    $('#modifyPassBtn').click(() => {
        let newPassPhoneIpt = $('.newPassPhoneIpt');
        let newPassPhoneIptVal = newPassPhoneIpt.val();
        let phoneTest = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        let phoneFlag = false;

        let passCode = $('.passCode');
        let passCodeVal = passCode.val();
        let codeTest = /^\d{4}$/;
        let codeFlag = false;

        let modifyPassIpt = $('.modifyPassIpt');
        let modifyPassIptVal = modifyPassIpt.val();
        let passTest = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;
        let passFlag = false;

        if (!codeTest.test(passCodeVal)) {
            $('#passCodeError').show().text('验证码格式不正确');
            codeFlag = false;
        } else {
            $('#passCodeError').hide();
            codeFlag = true;
        }

        if (!phoneTest.test(newPassPhoneIptVal)) {
            $('#passPhoneError').show().text('手机号格式不正确')
            phoneFlag = false;
        } else {
            $('#passPhoneError').hide();
            phoneFlag = true;
        }

        if (!passTest.test(modifyPassIptVal)) {
            $('#passError').show().text('密码格式为任意数字字母，6-12位')
            passFlag = false;
        } else {
            $('#passError').hide();
            passFlag = true;
        }

        if (phoneFlag && codeFlag && passFlag) {
            const securityPass = Interceptor('/securityPass', 'post', {
                'phone': newPassPhoneIptVal,
                'code': passCodeVal,
                'editPass': modifyPassIptVal
            })
            const { code, message } = securityPass

            if (code == 666) {
                successWindows('修改成功', '2秒后重新登陆')
                reviseSuccess()
            } else {
                errorWindows('修改密码', message)
            }
        }
    })

    //安全中心修改成功的回调函数
    function reviseSuccess() {
        window.sessionStorage.clear();
        window.localStorage.clear();

        setInterval(function() {
            window.location.replace('Login.html');
        }, 2000)
    }


    //跳转回安全中心
    function returnSecurity() {
        $('.security_con').show();
        $('#modifyPhone').hide();
        $('#modifyPass').hide();
        $('.modifyPassTitle').hide()
        $('.modifyPhoneTitle').hide()
    }

    //应聘人事
    const personnePostion = sessionStorage.getItem('postion')
    if (personnePostion == 1) {
        $('.user_nav ul li:last-of-type').text('人事审核')
        $('.personnelBox').show()
        $('.userBox').hide()
    }

    $('.applyBtn').click(function() {
        $('.image_code img').attr('src', 'http://172.16.1.241:8088/outImg?' + Math.random())
        const company_code = $('#company_code').val()
        const personnelCode = $('#personnelCode').val();

        const judgeImgCode = Interceptor('/judgeImgCode', 'post', {
            'invCode': company_code,
            'code': personnelCode
        })
        const { code, message } = judgeImgCode

        if (code != 666) {
            errorWindows('应聘人事', message)
        } else {
            successWindows('应聘人事', message)
        }
    })

    //退出登录
    $('.quitLogin').click(function() {
        successWindows('即将退出登录', '')
        setTimeout(() => {
            sessionStorage.clear()
            window.location.replace('Login.html')
        }, 1000)
    })

})